package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserSkinVolumeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/4/29 15:54
 */
public class UserSkinVolumeDataDaoImpl extends TempDataObjectDao<UserSkinVolumeData> {
    public static String insertSql = "insert into t_u_skin_volume(userId,embroideryMeter,piecesCount,skinWayGet,otherWayGet,usedPiecesCount) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserSkinVolumeData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEmbroideryMeter()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPiecesCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSkinWayGet()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOtherWayGet()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUsedPiecesCount()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    public static String updateSql = "update t_u_skin_volume set embroideryMeter=?, piecesCount=?, skinWayGet=?, otherWayGet=?, usedPiecesCount=? where userId=?";

    @Override
    protected boolean updateData(UserSkinVolumeData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getEmbroideryMeter()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPiecesCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSkinWayGet()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOtherWayGet()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUsedPiecesCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected UserSkinVolumeData parseData(ResultSet rs) throws SQLException {
        UserSkinVolumeData userSkinVolumeData = new UserSkinVolumeData();
        userSkinVolumeData.setUserId(rs.getLong("userId"));
        userSkinVolumeData.setEmbroideryMeter(rs.getInt("embroideryMeter"));
        userSkinVolumeData.setPiecesCount(rs.getLong("piecesCount"));
        userSkinVolumeData.setSkinWayGet(rs.getLong("skinWayGet"));
        userSkinVolumeData.setOtherWayGet(rs.getLong("otherWayGet"));
        userSkinVolumeData.setUsedPiecesCount(rs.getLong("usedPiecesCount"));
        return userSkinVolumeData;
    }

    public static String getSql = "select * from t_u_skin_volume where userId=?";
    public UserSkinVolumeData getSkinVolumeData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }
}
